﻿using Loki.Utils;

namespace Luna.Connectors.Google
{
    public class GoogleConnector : LokiObject, IConnector
    {
        public GoogleSettingsManager Manager { get; set; }

        public GoogleContactProvider Contacts { get; set; }

        private IGoogleSettings _Configuration;

        private bool _Activated;

        public ConnectorType Type
        {
            get { return ConnectorType.Google; }
        }

        public bool Enabled
        {
            get { return _Configuration != null && _Activated; }
        }

        public string Name
        {
            get { return _Configuration == null ? string.Empty : _Configuration.Login; }
        }


        public void Configure(IConnectorConfiguration P_Configuration)
        {
            IGoogleSettings L_Settings = P_Configuration as IGoogleSettings;
            if (L_Settings != null)
            {
                _Configuration = L_Settings;
                if (Test(P_Configuration) == string.Empty)
                {
                    Manager.Configure(L_Settings);
                    _Activated = true;
                }
                else
                {
                    _Activated = false;
                }
            }
            else
            {
                ResetConfig();
            }
        }

        public void ResetConfig()
        {
            _Configuration = null;
            _Activated = false;
        }

        public string Test(IConnectorConfiguration P_Configuration)
        {
            IGoogleSettings L_Settings = P_Configuration as IGoogleSettings;
            if (L_Settings != null)
            {
                return Contacts.Test(L_Settings);
            }
            else
            {
                return Resources.Errors.INVALID_CONFIGURATION;
            }
        }
    }
}